package com.cg.design.graphicdesignpattern.mediator;

import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

/**
 * 表示文本输入框的类，实现了Colleague，继承了java.awt.TextField类
 *
 * @program: LeetCode->ColleagueTextField
 * @description: 表示文本输入框的类，实现了Colleague类和java.awt.event.TextListener类，继承了java.awt.TextField类
 * @author: cg
 * @create: 2022-03-28 10:38
 **/
public class ColleagueTextField extends TextField implements TextListener, Colleague {
    /**
     * 中介者
     */
    private Mediator mediator;

    public ColleagueTextField(String text, int columns) {
        super(text, columns);
    }

    @Override
    public void setMediator(Mediator mediator) {
        // 保存Mediator
        this.mediator = mediator;
    }

    @Override
    public void setColleagueEnabled(boolean enabled) {
        // 根据中介者的指令下达启用/禁用
        setEditable(enabled);
        setBackground(enabled ? Color.white : Color.lightGray);
    }

    @Override
    public void textValueChanged(TextEvent event) {
        // 当文字发生变化时通知Mediator
        mediator.colleagueChanged();
    }

}
